#include <iostream>
using namespace std;
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdlib>
#include <vector>
#include <functional>
#include "Task.hpp"
#include "Channel.hpp"

// typedef std::funtion<void()> work_t;
using work_t = std::function<void()>;

enum
{
    OK = 0,
    UsageError,
    PipeError,
    ForkError
};

class ProcessPool
{
public:
    ProcessPool(int n, work_t w)
        : processnum(n), work(w) {};

    // channels输出型参数
    int InitProcessPool()
    {
        for (int i = 0; i < processnum; i++)
        {
            // 1.先有管道
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if (n < 0)
                return PipeError;

            // 2.创建进程
            pid_t id = fork();
            if (id < 0)
                return ForkError;

            // 3.建立通信信道
            if (id == 0)
            {
                // 关闭历史wfd
                cout << getpid() << ",child close history fd: ";
                for (auto &c : channels)
                {
                    cout << c.wfd() << " ";
                    c.Close();
                }
                cout << " over" << endl;

                ::close(pipefd[1]); // read
                // child
                dup2(pipefd[0], 0);
                work();
                ::exit(0);
            }

            // 父进程执行
            ::close(pipefd[0]); // write
            channels.emplace_back(pipefd[1], id);
            // Channel ch(pipefd[1],id);
            // channels.push_back(ch);
        }
        return OK;
    }

    void DispatchTask()
    {
        int num = 20;
        int who = 0;
        // 2.派发任务
        while (num--)
        {
            // a.选择一个任务,整数
            int task = tm.SelectTask();
            // b.选择一个子进程channel
            Channel &curr = channels[who++];
            who %= channels.size();

            cout << "################" << endl;
            cout << "send" << task << " to" << curr.Name() << " 任务还剩下: " << num << endl;
            cout << "################" << endl;
            // c.派发任务
            curr.Send(task);

            sleep(1);
        }
    }

    void CleanProcessPool()
    {
        // version 1
        for (auto &c : channels)
        {
            c.Close();
        }

        for (auto &c : channels)
        {
            pid_t rid = ::waitpid(c.Id(), nullptr, 0);
            if (rid > 0)
            {
                cout << "child " << rid << "wait ... success" << endl;
            }
        }

        // version 2
        // for(auto &c:channels)
        // for(int i = channels.size()-1;i>=0;i--)
        // {
        //     channels[i].Close();
        //     pid_t rid = ::waitpid(channels[i].Id(), nullptr, 0);
        //     if (rid > 0)
        //     {
        //         cout << "child " << rid << "wait ... success" << endl;
        //     }
        // }

        // version 1
        // for (auto &c : channels)
        // {
        //     c.Close();
        // }

        // for (auto &c : channels)
        // {
        //     pid_t rid = ::waitpid(c.Id(), nullptr, 0);
        //     if (rid > 0)
        //     {
        //         cout << "child " << rid << "wait ... success" << endl;
        //     }
        // }
    }

    void DebugPrint()
    {
        for (auto &c : channels)
        {
            cout << c.Name() << endl;
        }
    }

private:
    vector<Channel> channels;
    int processnum;
    work_t work;
};
